package com.hkts.naturalgasauthentication.utils;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassNameAliyunSmsUtils
 * @Description
 * @Author yck
 * @Date2021-03-17 16:53
 * @Version V1.0
 **/
@Slf4j
@Component
public class AliyunSmsUtils {
    //@Value("${aliyun.sms.accessKeyId}")
    private String  accessKeyId="LTAI4FqfcbzKxzaHZCXrVUYB";

   // @Value("${aliyun.sms.signName}")
    private String  signName="鸿科碳素资源共享";

    //@Value("${aliyun.sms.template_code}")
    private String  template_code="SMS_187226508";
    //@Value("${aliyun.sms.product}")
    private String  product="Dysmsapi";
    //@Value("${aliyun.sms.accessKeySecret}")
    private String  accessKeySecret="jF0B9htrJeWL3xX9C0S3vwKEAFkF5I";
    //@Value("${aliyun.sms.domain}")
    private String  domain="dysmsapi.aliyuncs.com";

    private String  regionId="cn-hangzhou";
    public CommonResponse sendSms(String mobile, int code) throws ClientException {
        Pageable pageable = PageRequest.of(0,1, Sort.Direction.DESC,"smsConfigId");

        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain(domain);
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", regionId);
        request.putQueryParameter("PhoneNumbers", mobile);
        request.putQueryParameter("SignName", signName);
        request.putQueryParameter("TemplateCode", template_code);
        request.putQueryParameter("TemplateParam", "{\"code\":\""+code+"\"}");
        Map<String,Object> map=new HashMap<String,Object>();
        CommonResponse data=null;
        try {
            data = client.getCommonResponse(request);
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return data;
    }
}
